package com.jk.myimkit.imkit.fragment;

import android.os.Message;
import android.text.TextUtils;
import android.view.View;

import com.jk.myimkit.R;
import com.jk.myimkit.imkit.RongContext;
import com.jk.myimkit.imkit.RongIM;
import com.jk.myimkit.imkit.model.Event;

import io.rong.common.RLog;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;

public class SetConversationToTopFragment extends BaseSettingFragment {
    private static String TAG = SetConversationToTopFragment.class.getSimpleName();

    public SetConversationToTopFragment() {
    }

    protected void initData() {
        if (RongContext.getInstance() != null) {
            RongContext.getInstance().getEventBus().register(this);
        }

        RongIM.getInstance().getConversation(this.getConversationType(), this.getTargetId(), new RongIMClient.ResultCallback<Conversation>() {
            public void onSuccess(Conversation conversation) {
                if (conversation != null) {
                    SetConversationToTopFragment.this.setSwitchBtnStatus(conversation.isTop());
                }

            }

            public void onError(RongIMClient.ErrorCode e) {
            }
        });
    }

    protected boolean setSwitchButtonEnabled() {
        return true;
    }

    protected String setTitle() {
        return this.getString(R.string.rc_setting_set_top);
    }

    public boolean handleMessage(Message msg) {
        return false;
    }

    protected void onSettingItemClick(View v) {
        RLog.i(TAG, "onSettingItemClick, " + v.toString());
    }

    protected int setSwitchBtnVisibility() {
        return 0;
    }

    protected void toggleSwitch(boolean toggle) {
        if (this.getConversationType() != null && !TextUtils.isEmpty(this.getTargetId())) {
            RongIM.getInstance().setConversationToTop(this.getConversationType(), this.getTargetId(), toggle, (RongIMClient.ResultCallback)null);
        } else {
            RLog.e(TAG, "toggleSwitch() args is null");
        }

    }

    public void onEventMainThread(Event.ConversationTopEvent conversationTopEvent) {
        if (conversationTopEvent != null && conversationTopEvent.getTargetId().equals(this.getTargetId()) && conversationTopEvent.getConversationType().getValue() == this.getConversationType().getValue()) {
            this.setSwitchBtnStatus(conversationTopEvent.isTop());
        }

    }

    public void onDestroy() {
        if (RongContext.getInstance() != null) {
            RongContext.getInstance().getEventBus().unregister(this);
        }

        super.onDestroy();
    }
}
